﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using KBEngine.Setting.Internal;

namespace KBEngine.Setting
{
    [Serializable]
    [XmlRoot("root")]
    public class KBEngineDefaults
    {
        /// <summary>
        /// 游戏逻辑处理频率HZ
        /// </summary>
        public short gameUpdateHertz = 10;

        /// <summary>
        /// 每秒发送到客户端的带宽限制(bit) 
        /// </summary>
        public uint bitsPerSecondToClient = 20000;

        /// <summary>
        /// 非0则不优化 (bool)
        /// </summary>
        public int packetAlwaysContainLength = 0;

        /// <summary>
        /// 是否需要将任何接收和发送的包以文本输出到log中提供调试
        /// </summary>
        public TracePacket trace_packet = new TracePacket();

        /// <summary>
        /// 是否输出entity的创建， 脚本获取属性， 初始化属性等调试信息， 以及def信息(bool)
        /// </summary>
        public int debugEntity = 0;
        public Publish publish = new Publish();
        public ChannelCommon channelCommon = new ChannelCommon();
        public float shutdown_time = 30.0f;
        public float shutdown_waittick = 1.0f;
        public float callback_timeout = 300.0f;
        public Thread_Pool thread_pool = new Thread_Pool();
        public string email_service_config = "server/email_service_defaults.xml";

        public Interfaces interfaces = new Interfaces();
        public DBmgr dbmgr = new DBmgr();
        public CellApp cellapp = new CellApp();
        public BaseApp baseapp = new BaseApp();
        public CellAppMgr cellappmgr = new CellAppMgr();
        public BaseAppMgr baseappmgr = new BaseAppMgr();
        public LoginApp loginapp = new LoginApp();
        public Machine machine = new Machine();
        public Bots bots = new Bots();
        public Internal.Logger logger = new Internal.Logger();

        [Serializable]
        public class TracePacket
        {
            public byte debug_type = 0;
            public bool use_logfile = false;
            [XmlArrayItem("item")]
            public List<string> disables = new List<string>();
        }
        [Serializable]
        public class Publish
        {
            /// <summary>
            /// 发布状态  0-debug  1-release  其他自定义
            /// </summary>
            public sbyte state = 0;

            /// <summary>
            /// 脚本层发布的版本号
            /// </summary>
            public string script_version = "0.1.0";
        }

        [Serializable]
        public class ChannelCommon
        {
            /// <summary>
            /// 最后一次通信时间超过此时间则被认定为超时通道， 服务器将踢出该通道（秒）
            /// </summary>
            public InOutSideFloat timeout = new InOutSideFloat();

            /// <summary>
            /// TCP socket读取缓冲大小
            /// </summary>
            public InOutSide readBufferSize = new InOutSide();

            /// <summary>
            /// TCP socket写入缓冲大小
            /// </summary>
            public InOutSide writeBufferSize = new InOutSide();

            /// <summary>
            /// 发送与接收窗口溢出值，0无限制
            /// 一个tick内发送的字节数溢出限制 
            /// </summary>
            public WindowOverflow windowOverflow = new WindowOverflow();

            /// <summary>
            /// 加密通信，只对外部通道
            ///    可选择的加密方式(Optional encryption) :
            ///		0: 无加密(No Encryption)
            ///		1: Blowfish
            ///		2: RSA(res\key\kbengine_private.key)
            /// </summary>
            public sbyte encrypt_type = 1;
            public ReliableUDP reliableUDP = new ReliableUDP();

            /// <summary>
            /// HTTPS/Wss/SSL通信所需的证书文件。
            /// </summary>
            public string sslCertificate = "key/server_cert.pem";
            public string sslPrivateKey = "key/server_key.pem";
        }
        [Serializable]
        public class Thread_Pool
        {
            public float timeout = 300.0f;
            public uint init_create = 1;
            public uint pre_create = 2;
            public uint max_create = 8;
        }

        [Serializable]
        public class ReliableUDP
        {
            /// <summary>
            /// 相当于TCP RCV_BUF, UDP包的数量
            /// </summary>
            public InOutSide readPacketsQueueSize = new InOutSide();
            public InOutSide writePacketsQueueSize = new InOutSide();

            /// <summary>
            /// 内部更新定时器间隔(毫秒)
            /// </summary>
            public uint tickInterval = 10;

            /// <summary>
            /// 重传超时时间(毫秒)
            /// </summary>
            public uint minRTO = 10;

            /// <summary>
            /// ACK跳过次数
            /// </summary>
            public uint missAcksResend = 10;
            public uint mtu = 10;

            /// <summary>
            /// False:关闭堵塞控制
            /// </summary>
            public bool congestionControl = false;
            public bool nodelay = true;
        }
        [Serializable]
        public class WindowOverflow
        {
            public Send send = new Send();
            public Receive receive = new Receive();

            [Serializable]
            public class Send
            {
                public InOutSide tickSentBytes = new InOutSide();
                public Messages messages = new Messages();
                public InOutSide bytes = new InOutSide();
            }
            [Serializable]
            public class Receive
            {
                public Messages messages = new Messages();
                public InOutSide bytes = new InOutSide();
            }
        }
        [Serializable]
        public class InOutSide
        {
            [XmlElement("internal")]
            public int internall;
            public int external;
        }
        [Serializable]
        public class InOutSideFloat
        {
            [XmlElement("internal")]
            public float internall;
            public float external;
        }
        [Serializable]
        public class Messages : InOutSide
        {
            public int critical;
        }
    }

    [Serializable]
    public class TelnetService
    {
        public uint port = 0;
        public string password = "pwd123456";
        public string default_layer = "python";
        public override string ToString()
        {
            return port.ToString();
        }
    }

    /// <summary>
    /// 如果设置为true，引擎启动时就会开始记录相关profile信息， 进程退出后将导出一份报告
    /// </summary>
    [Serializable]
    public class Profiles
    {
        public bool cprofile = false;
        public bool pyprofile = false;
        public bool eventprofile = false;
        public bool networkprofile = false;
    }
}
